package com.sky.controller.admin;

import com.sky.properties.AliOssProperties;
import com.sky.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
@RequestMapping("/admin/common")
public class UpLoadController {

    @Autowired
    private AliOssProperties aliOssProperties;

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        try {
            AliOssUtil aliOssUtil = new AliOssUtil(
                    aliOssProperties.getEndpoint(),
                    aliOssProperties.getAccessKeyId(),
                    aliOssProperties.getAccessKeySecret(),
                    aliOssProperties.getBucketName()
            );

            String originalFilename = file.getOriginalFilename();
            log.info("接收到上传文件: {}", originalFilename);

            // 上传文件
            String url = aliOssUtil.upload(file.getBytes(), originalFilename);

            log.info("文件上传成功，访问URL: {}", url);
            return url;

        } catch (Exception e) {
            log.error("文件上传失败", e);
            return "上传失败: " + e.getMessage();
        }
    }
}
